package com.atguigu.dga.ds.service.impl;

import com.atguigu.dga.ds.bean.TDsTaskInstance;
import com.atguigu.dga.ds.mapper.TDsTaskInstanceMapper;
import com.atguigu.dga.ds.service.TDsTaskInstanceService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuzhida
 * @since 2024-06-30
 */
@Service
@DS("dolphinscheduler")
public class TDsTaskInstanceServiceImpl extends ServiceImpl<TDsTaskInstanceMapper, TDsTaskInstance> implements TDsTaskInstanceService {

    /**
     * 提取任务实例
     * @param assessDate
     * @return
     */
    @Override
    public List<TDsTaskInstance> getTDsTaskInstanceList(String assessDate) {
        return list(
                new QueryWrapper<TDsTaskInstance>()
                        .inSql("id", "\tSELECT MAX(id) max_id \n" +
                                "\tFROM t_ds_task_instance \n" +
                                "\tWHERE task_type = 'SHELL'\n" +
                                "\tAND state = 7\n" +
                                "\tAND DATE_FORMAT(start_time, '%Y-%m-%d') = '" + assessDate + "' GROUP BY name")
        );
    }

    /**
     * 查询指定表指定考评日期是否有报错的实例
     * @param name
     * @param assessDate
     * @return
     */
    @Override
    public List<TDsTaskInstance> getFailTDsTaskInstanceList(String name, String assessDate) {
        return list(
                new QueryWrapper<TDsTaskInstance>()
                        .eq("name", name)
                        .eq("DATE_FORMAT(start_time, '%Y-%m-%d')", assessDate)
                        .eq("state", 6)
        );
    }

    /**
     * 查询指定表前几天的任务实例
     * @param name
     * @param startDate
     * @param assessDate
     * @return
     */
    @Override
    public List<TDsTaskInstance> getBeforeDaystDsTaskInstanceList(String name, String startDate, String assessDate) {
        return list(
                new QueryWrapper<TDsTaskInstance>()
                        .eq("id", "\tSELECT \n" +
                                "\tMAX(id) max_id\n" +
                                "\tFROM t_ds_task_instance\n" +
                                "\tWHERE name = 'gmall.ads_user_action'\n" +
                                "\tAND DATE_FORMAT(submit_time, '%Y-%m-%d') >= '2023-04-27' \n" +
                                "\tAND DATE_FORMAT(end_time, '%Y-%m-%d') < '2023-05-02' \n" +
                                "\tAND state = 7\n" +
                                "\tGROUP BY name, start_time")
                
        );
    }
}
